# Maintainer: Rafal Brzegowy <rafal.brzegowy@yahoo.com>
# Contributor: Raed Rizqie <raed.rizqie@gmail.com>

_realname=precice
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=3.2.0
pkgrel=1
pkgdesc='A Coupling Library for Partitioned Multi-Physics Simulations on Massively Parallel Systems (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.precice.org'
msys2_repository_url='https://github.com/precice/precice'
license=('spdx:LGPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-boost-libs"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         #"${MINGW_PACKAGE_PREFIX}-petsc"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-numpy")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             $([[ ${CARCH} == aarch64 ]] || echo "${MINGW_PACKAGE_PREFIX}-msmpi")
             #"${MINGW_PACKAGE_PREFIX}-ninja"
             #"${MINGW_PACKAGE_PREFIX}-petsc-build"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-eigen3"
             "git")
source=(https://github.com/precice/precice/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz
        001-fix-libprecice-dll-installation.patch
        002-fix-export-defs.patch::https://github.com/precice/precice/commit/f7a4723827bbf20062fe004322b837f348374af3.patch
        003-add-win32-deps.patch::https://github.com/precice/precice/commit/2d08cf23abe81490f11c2be0e00afd43648ee504.patch)
sha256sums=('93523f1a56e0cfd338d8e190baa06129ee811acdb1c697468a3c85c516d63464'
            '49fe04f9e72d1034b8909a7b593ccf42eeaf5203d9188f71706260948a0fb566'
            '8efe809bf94f93d2fa0bcdf6cc95cda4028a469b5bd88c5a1a7b0738e615e05a'
            '786d2f0be4e6131192501bf81d574ca5b3839e6de95a78de3ceea0738c7fa81a')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd ${_realname}-${pkgver}
  apply_patch_with_msg \
    001-fix-libprecice-dll-installation.patch \
    002-fix-export-defs.patch \
    003-add-win32-deps.patch
}

build() {
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # . ${MINGW_PREFIX}/src/petsc/petsc
  # for build in ${petsc_builds}; do
  #   #export PETSC_ARCH=${build}
  #   export PETSC_ARCH=dmo
  #   export PETSC_DIR=${MINGW_PREFIX}/src/petsc
  # done

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ ${CARCH} == aarch64 ]]; then
    extra_config+=("-DPRECICE_FEATURE_MPI_COMMUNICATION=OFF")
  else
    extra_config+=("-DPRECICE_FEATURE_MPI_COMMUNICATION=ON")
  fi

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    CXXFLAGS+=" -Wno-c++11-narrowing"
  else
    CXXFLAGS+=" -Wno-narrowing"
  fi

  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G"Ninja" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DBUILD_SHARED_LIBS=ON \
      -DBUILD_TESTING=OFF \
      ${extra_config[@]} \
      -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
      -DPRECICE_FEATURE_PETSC_MAPPING=OFF \
      -DPRECICE_FEATURE_PYTHON_ACTIONS=ON \
      -DPRECICE_BUILD_TOOLS=ON \
      -Wno-dev \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
